package cn.bs.busuo.account.service.impl;

import cn.bs.busuo.account.dao.persist.repository.IPermissionRepository;
import cn.bs.busuo.account.pojo.entity.UserRole;
import cn.bs.busuo.account.pojo.param.Role;
import cn.bs.busuo.account.pojo.vo.UserRoleVO;
import cn.bs.busuo.account.pojo.vo.UserVO;
import cn.bs.busuo.account.service.IPermissionService;
import cn.bs.busuo.common.enumerator.ServiceCode;
import cn.bs.busuo.common.ex.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private IPermissionRepository permissionRepository;

    //新增景点管理员
    @Override
    public void insertById(Long id) {
        List<UserRoleVO> listUserRole = permissionRepository.selectById(id);
        UserVO userVO = permissionRepository.selectUser(id);
        if (userVO == null) {
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, "不存在");
        }
        for (UserRoleVO userRole : listUserRole) {
            if (userRole.getRoleId() == 5) {
                String message = "增加失败，该用户已拥有权限！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
            }
        }
        UserRole userRole = new UserRole();
        userRole.setRoleId(5L);
        userRole.setUserId(id);
        System.out.println(userRole);
        permissionRepository.insertById(userRole);
    }

    //删除管理员
    @Override
    public void deleteById(Long id) {
        UserVO userVO = permissionRepository.selectUser(id);
        if (userVO == null) {
            String message = "删除失败，该用户不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Role role = new Role();
        role.setUserId(id);
        role.setRoleId(5L);
        permissionRepository.deleteById(role);

    }

    //新增工作人员
    @Override
    public void addOperator(Long id) {
        List<UserRoleVO> listUserRole = permissionRepository.selectById(id);
        UserVO userVO = permissionRepository.selectUser(id);
        if (userVO == null) {
            String message = "增加失败，该用户不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        for (UserRoleVO userRole : listUserRole) {
            if (userRole.getRoleId() == 7) {
                String message = "增加失败，该用户已拥有权限！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
            }
        }
        UserRole userRole = new UserRole();
        userRole.setRoleId(7L);
        userRole.setUserId(id);
        System.out.println(userRole);
        permissionRepository.insertById(userRole);
    }

    //删除工作人员
    @Override
    public void delectOperator(Long id) {
        UserVO userVO = permissionRepository.selectUser(id);
        if (userVO == null) {
            String message = "删除失败，该用户不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Role role = new Role();
        role.setUserId(id);
        role.setRoleId(7L);
        permissionRepository.deleteById(role);

    }
}
